{% load static %}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="{% static 'admin/css/pearCommon.css' %}" />
    <link rel="stylesheet" href="{% static 'component/layui/css/layui.css' %}" />
</head>

<body>
    <form class="layui-form" action="">
        {% csrf_token %}
        <div class="mainBox">
            <div class="main-container">
                <div class="main-container">
                    <div class="layui-form-item">
                        <label class="layui-form-label">选择物料</label>
                        <div class="layui-input-block">
                            <select name="supply_id" lay-verify="required" lay-filter="role">
                                <option value=""></option>
                                {% for sp in supplies %}
                                <option value="{{sp.id}}">{{ sp.name }}</option>
                                {% endfor %}
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">物料数量</label>
                        <div class="layui-input-block">
                            <input type="text" name="number" lay-verify="required|number" autocomplete="off"
                                placeholder="请输入物料数量" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">图片上传</label>
                        <div class="layui-input-block">
                            <div class="layui-upload-drag" id="upload_img">
                                <i class="layui-icon"></i>
                                <p>点击上传，或将文件拖拽到此处</p>
                                <div class="layui-hide" id="uploadDemoView">
                                    <hr>
                                    <img src="" alt="上传成功后渲染" style="max-width: 250px">
                                </div>
                            </div>
                        </div>
                    </div>
                    <input type="hidden" name="img_url">
                    <div class="layui-form-item">
                        <label class="layui-form-label">备注</label>
                        <div class="layui-input-block">
                            <textarea name="remark" placeholder="请输入备注" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="bottom">
            <div class="button-container">
                <button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit=""
                    lay-filter="user-save">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
                <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </div>
    </form>
    <script src="{% static 'component/layui/layui.js' %}"></script>
    <script src="{% static 'component/my.js' %}"></script>
    <script>
        layui.use(['form', 'jquery', 'upload'], function () {
            let form = layui.form;
            let $ = layui.jquery;
            let upload = layui.upload;

            //拖拽上传
            upload.render({
                elem: '#upload_img',
                accept: "images",   // 指定允许上传时校验的文件类型
                url: "{% url 'home:upload' %}",    //改成您自己的上传接口
                headers: {
                        "X-CSRFToken": getCookie("csrftoken")
                },
                done: function (res) {
                    layer.msg('上传成功');
                    layui.$('#uploadDemoView').removeClass('layui-hide').find('img').attr('src', res.file_url);
                    $("input[name=img_url]").val(res.file_url)
                }
            });

            form.on('submit(user-save)', function (data) {
                let url = "{% url 'supply:record_add' 'order_type' %}"
                url = url.replace("order_type", 2)
                $.ajax({
                    url: url,
                    data: JSON.stringify(data.field),
                    dataType: 'json',
                    contentType: 'application/json',
                    type: 'post',
                    headers: {
                        "X-CSRFToken": getCookie("csrftoken")
                    },
                    success: function (result) {
                        console.log(result)
                        if (result.status == 1) {
                            layer.msg(result.msg, { icon: 1, time: 1000 }, function () {
                                parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                                parent.layui.table.reload("user-table");
                            });
                        } else {
                            layer.msg(result.msg, { icon: 2, time: 1000 });
                        }
                    }
                })
                return false;
            });
        })
    </script>
    <script>
    </script>
</body>

</html>